<template>

	<view class="content">
		<tn-toast ref="toast"></tn-toast>
		<view class="content_top">
			<image class="background-image" src="../../static/img/bg.png"></image>
			<view class="content_top_box">
				<view class="content_top_box_left">
					<view class="content_top_box_left_hi">
						Hi~
					</view>
					<view class="content_top_box_left_name">
						{{name}}
					</view>
				</view>
				<view class="content_top_box_right">

					<tn-avatar size="xl" @click="onImageClick(RLZP_FJ)" :src="RLZP_FJ"></tn-avatar>
				</view>
			</view>
		</view>
		<view class="content_bottom" :style="{ height: dynamicHeight }">
			<view class="content_bottom_xm" v-if="showNull " style="text-align: center;">
				<image src="../../static/img/nullproject.png" mode="widthFix" style="width: 270rpx;"></image>
			</view>
			<view class="content_bottom_xm" v-else>
				<view class="content_bottom_xm_img">
					<image src="../../static/img/indexbac.png" mode="widthFix" style="width: 134rpx;height: 146rpx;">
					</image>
				</view>

				<view class="content_bottom_xm_name">
					项目名称：{{BIDDING_PROJECT_NAME}}
				</view>
				<view class="content_bottom_xm_fjname">
					分建工程名称：{{SUBPACKAGE_PROJECT_NAME}}
				</view>
				<view class="content_bottom_xm_dept">
					单位：{{AFFILIATED_PROJECT_FIRM}}
				</view>
				<view class="content_bottom_xm_time">
					首次进场时间：{{firstTime}}
				</view>
			</view>
			<view class="content_bottom_list">
				<view class="content_bottom_list_dj" @click="Entry()">
					<view class="content_bottom_list_dj_left">
						<view class="content_bottom_list_dj_left_name">
							进场
						</view>
						<view class="content_bottom_list_dj_left_detail">
							扫码进入工地
						</view>
					</view>
					<view class="content_bottom_list_dj_right">
						<image src="../../static/img/entry.png" style="width: 103rpx;height: 98rpx;"></image>
					</view>
				</view>

				<view class="content_bottom_list_dj" @click="exit()">
					<view class="content_bottom_list_dj_left">
						<view class="content_bottom_list_dj_left_name">
							退场
						</view>
						<view class="content_bottom_list_dj_left_detail">
							点击退出工地
						</view>
					</view>
					<view class="content_bottom_list_dj_right">
						<image src="../../static/img/exit.png" style="width: 89rpx;height: 79rpx;"></image>
					</view>
				</view>
				<view class="content_bottom_list_dj" @click="getphoto()">
					<view class="content_bottom_list_dj_left">
						<view class="content_bottom_list_dj_left_name">
							随手拍
						</view>
						<view class="content_bottom_list_dj_left_detail">
							点击进行拍照
						</view>
					</view>
					<view class="content_bottom_list_dj_right">
						<image src="../../static/img/getphoto.png" style="width: 99rpx;height: 84rpx;"></image>
					</view>
				</view>
			<!-- 	<view class="content_bottom_list_dj" @click="delsession()">
					<view class="content_bottom_list_dj_left">
						<view class="content_bottom_list_dj_left_name">
							清除缓存
						</view>
						<view class="content_bottom_list_dj_left_detail">
							点击清除缓存
						</view>
					</view>
					<view class="content_bottom_list_dj_right">
						<image src="../../static/img/getphoto.png" style="width: 99rpx;height: 84rpx;"></image>
					</view>
				</view> -->
			</view>

			<view class="content_bottom_title">
				上海市基础工程集团有限公司
			</view>
		</view>
		<TabBar selectedIndex=1></TabBar>
	</view>
</template>


<script>
	import {
		BASE_URL
	} from '../../utils/request.js'
	import TabBar from "../../components/tabbar.vue";

	export default {
		components: {
			TabBar
		},
		computed: {
			//判断是什么角色
			selectedTabIndex() {
				let a = 'tom'
				return a === 'tom' ? 1 : 2;
			},
		},

		data() {
			return {
				isUpdate: '',
				ZT: '',
				isQuitApply: '0',
				BUYING_TASK_NO: '',
				dynamicHeight: '', // 默认高度样式
				PHONE_NUMBER: '',
				USER_NAME: '',
				PROJECT_NO: '',
				isApproachApply: '0',
				name: '',
				RLZP_FJ: '',
				BIDDING_PROJECT_NAME: '',
				SUBPACKAGE_PROJECT_NAME: '',
				AFFILIATED_PROJECT_FIRM: '',
				firstTime: '',
				showNull: false,
				isTimeZero: false, // 标志位，用于控制文本显示
				entryShow: false, // 控制弹窗显示
				isButtonDisabled: true, // 控制按钮是否可点击
				countdown: 10,
				remainingTime: 10,
				buttonBackgroundColor: '#CCCCCC' // 初始按钮背景色
			}
		},
		onLoad() {
			uni.getSystemInfo({
				success: (info) => {
					console.log(info.screenHeight, 'info.screenHeight')
					if (info.screenHeight <= 667) { // 假设iPhone X的高度是812像素
						this.dynamicHeight = 'calc(100% + 200rpx)'; // 如果设备高度小于iPhone X，则添加额外的200rpx
					} else {
						this.dynamicHeight = 'calc(100% - 200rpx)';
					}
				},
			});

		},
		onShow() {
			//⭐隐藏pages.json里配置的导航栏，使用封装的tabbar组件
			uni.hideTabBar({
				animation: false
			})
			this.getdetail();
		},
		mounted() {
			this.getdetail();
		},
		methods: {
			//清除缓存
			async delsession() {

				try {
					const url = '/portal/r/w'; // 替换为你的 API 地址
					const params = {
						// 构造你的请求参数
						// sid: uni.getStorageSync('SID'),
						sid: uni.getStorageSync('SID'),
						cmd: 'sfeg.srm.workers.join.apply.queryApproachInformation',
						openId: uni.getStorageSync('OPEN_ID'),
						phone: uni.getStorageSync('phoneNumber')
					};

					// 发送 POST 请求
					const response = await this.$get(url, params);

					// 处理请求成功的逻辑
					console.log('请求成功个人数据', response);
					if (response.msg == '没有查询到该员工信息') {
						this.$refs.toast.show({

							content: '没有查询到该员工信息！',
							icon: 'error', // 不显示图标
							duration: 2000 // 显示时长 2000ms
						});

						uni.removeStorageSync('SID');
						uni.removeStorageSync('UNIONID');
						uni.removeStorageSync('OPEN_ID');
						uni.removeStorageSync('IS_LEADER');
						uni.removeStorageSync('scrollHeight');
						uni.removeStorageSync('phoneNumber');
						uni.removeStorageSync('BOID');
						uni.removeStorageSync('IMG_NAME');
						uni.removeStorageSync('SF_GL');
						uni.removeStorageSync('wxcd');
						uni.removeStorageSync('wxcdID');
						uni.removeStorageSync('fsignPhoto');
						uni.removeStorageSync('fsignPhotoName');
						uni.removeStorageSync('savedData');
						uni.removeStorageSync('AGE');
						uni.removeStorageSync('DOWNLOADURL');
						uni.removeStorageSync('DOWNLOADURLName');
						uni.removeStorageSync('ID_CARD');
						uni.removeStorageSync('NATION');
						uni.removeStorageSync('SEX');
						uni.removeStorageSync('USER_NAME');
						uni.removeStorageSync('DOWNLOADURLDown');
						uni.removeStorageSync('lwphoto');
						uni.removeStorageSync('lwphotoName');
						uni.removeStorageSync('photoBOID');
						uni.redirectTo({
							url: '../loginIndex/loginIndex'
						});

					}  else {
						this.$refs.toast.show({

							content: '清除成功！',
							icon: 'error', // 不显示图标
							duration: 2000 // 显示时长 2000ms
						});
					}


				} catch (error) {
					// 处理请求失败的逻辑
					console.error('请求失败', error);
				}
			},
			onImageClick(imageUrl) {
				console.log(imageUrl, 'imageUrl')
				const current = imageUrl;
				uni.previewImage({
					current: current, // 当前显示图片的链接
					// urls 数组只需要包含当前图片的URL，因为只预览一张
					urls: [current]
				});

			},
			async getdetail() {
				try {
					const url = '/portal/r/w'; // 替换为你的 API 地址
					const params = {
						// 构造你的请求参数
						// sid: uni.getStorageSync('SID'),
						sid: uni.getStorageSync('SID'),
						cmd: 'sfeg.srm.workers.join.apply.queryApproachInformation',
						openId: uni.getStorageSync('OPEN_ID'),
						phone: uni.getStorageSync('phoneNumber')
					};

					// 发送 POST 请求
					const response = await this.$get(url, params);
					if (response.msg == '没有查询到该员工信息') {
						this.$refs.toast.show({
					
							content: '没有查询到该员工信息！',
							icon: 'error', // 不显示图标
							duration: 2000 // 显示时长 2000ms
						});
					
						uni.removeStorageSync('SID');
						uni.removeStorageSync('UNIONID');
						uni.removeStorageSync('OPEN_ID');
						uni.removeStorageSync('IS_LEADER');
						uni.removeStorageSync('scrollHeight');
						uni.removeStorageSync('phoneNumber');
						uni.removeStorageSync('BOID');
						uni.removeStorageSync('IMG_NAME');
						uni.removeStorageSync('SF_GL');
						uni.removeStorageSync('wxcd');
						uni.removeStorageSync('wxcdID');
						uni.removeStorageSync('fsignPhoto');
						uni.removeStorageSync('fsignPhotoName');
						uni.removeStorageSync('savedData');
						uni.removeStorageSync('AGE');
						uni.removeStorageSync('DOWNLOADURL');
						uni.removeStorageSync('DOWNLOADURLName');
						uni.removeStorageSync('ID_CARD');
						uni.removeStorageSync('NATION');
						uni.removeStorageSync('SEX');
						uni.removeStorageSync('USER_NAME');
						uni.removeStorageSync('DOWNLOADURLDown');
						uni.removeStorageSync('lwphoto');
						uni.removeStorageSync('lwphotoName');
						uni.removeStorageSync('photoBOID');
						uni.redirectTo({
							url: '../loginIndex/loginIndex'
						});
						
					
					}else{
							uni.setStorageSync('USER_NAME', response.data.USER_NAME);
							uni.setStorageSync('lwphoto', response.data.RLZP_FJ);
							// 处理请求成功的逻辑
							console.log('请求成功个人数据', response);
							this.name = response.data.USER_NAME;
							this.isApproachApply = response.data.isApproachApply;
							this.RLZP_FJ = BASE_URL + '/portal/r' + response.data.RLZP_FJ.slice(1);
							this.isQuitApply = response.data.isQuitApply;
							this.isUpdate = response.data.isUpdate;
							this.ZT = response.data.ZT;
							
							if (response.data.BIDDING_PROJECT_NAME == '' || response.data.BIDDING_PROJECT_NAME == undefined) {
								this.showNull = true;
							} else {
								this.showNull = false;
							
								this.BIDDING_PROJECT_NAME = response.data.BIDDING_PROJECT_NAME;
								this.SUBPACKAGE_PROJECT_NAME = response.data.SUBPACKAGE_PROJECT_NAME;
								this.AFFILIATED_PROJECT_FIRM = response.data.AFFILIATED_PROJECT_FIRM;
								this.firstTime = response.data.firstTime;
								this.PROJECT_NO = response.data.PROJECT_NO;
								this.PHONE_NUMBER = response.data.PHONE_NUMBER;
								this.USER_NAME = response.data.USER_NAME;
								this.BUYING_TASK_NO = response.data.BUYING_TASK_NO;
							
							
							}
					}
				
				


				} catch (error) {
					// 处理请求失败的逻辑
					console.error('请求失败', error);
				}
			},

			getphoto() {
				var that = this;
				if (that.isApproachApply == '1' && that.ZT == '1') {
					uni.getLocation({
						type: 'wgs84', // 默认为 wgs84 的 gps 坐标，如果要返回直接给 openLocation 用的火星坐标，可传入 'gcj02'
						success: async function(location) {
							console.log('获取位置成功', location);
							// location 对象包含经纬度信息：latitude（纬度）和 longitude（经度）
							var latitude = location.latitude;
							var longitude = location.longitude;

							try {
								const url = '/portal/r/w'; // 替换为你的 API 地址
								const params = {
									// 构造你的请求参数
									sid: uni.getStorageSync('SID'),
									cmd: 'sfeg.srm.workers.join.apply.snapshotCheck',



									buyingTaskNo: that.BUYING_TASK_NO,
									latitude: longitude + ',' + latitude
								};

								// 发送 POST 请求
								const response = await that.$get(url, params);
								let latlong = longitude + ',' + latitude;
								uni.setStorageSync('latlong', latlong);
								// 处理请求成功的逻辑
								console.log('出入登记成功', response);

								let obj = response.data;
								if (response.result == 'ok') {
									uni.setStorageSync('imgbinb', 1);
									uni.navigateTo({
										url: '../randomClapping/randomClapping'
									});
								} else {
									that.$refs.toast.show({

										content: '不在范围内，无法进行随手拍，请确认！',
										icon: 'error', // 不显示图标
										duration: 2000 // 显示时长 2000ms
									});

								}



							} catch (error) {
								// 处理请求失败的逻辑
								console.error('请求失败', error);
							}
						},
						fail: function(err) {
							console.error('获取位置失败', err);
							that.$refs.toast.show({

								content: '定位失败，请检查您的位置信息设置',
								icon: 'error', // 不显示图标
								duration: 2000 // 显示时长 2000ms
							});
							// 定位失败可以执行的操作，比如提示用户


							// 仍然可以导航到错误页面
							// uni.navigateTo({
							//   url: '../registrationError/registrationError'
							// });
						}
					});
				} else {
					that.$refs.toast.show({

						content: '您当前没有进场的记录，无法进行随手拍，请确认！',
						icon: 'error', // 不显示图标
						duration: 2000 // 显示时长 2000ms
					});
				}
			},
			// uni.navigateTo({
			// 	url: '../../pages/randomClapping/randomClapping'
			// });

			exit() {
				if (this.isUpdate == '1') {
					this.$refs.toast.show({

						content: '信息变更未完成，禁止退场',
						icon: 'error', // 不显示图标
						duration: 2000 // 显示时长 2000ms
					});
					return;
				}

				if (this.isQuitApply == '0') {
					var obj = {

						BIDDING_PROJECT_NAME: this.BIDDING_PROJECT_NAME,
						SUBPACKAGE_PROJECT_NAME: this.SUBPACKAGE_PROJECT_NAME,
						PROJECT_NO: this.BUYING_TASK_NO,
						PHONE_NUMBER: this.PHONE_NUMBER,
						USER_NAME: this.USER_NAME
					}
					uni.navigateTo({
						url: '../Exit/Exit?data=' + JSON.stringify(obj)

					});
				} else if (this.isQuitApply == '1' && this.ZT == '') {
					this.$refs.toast.show({

						content: '您还未进场',
						icon: 'error', // 不显示图标
						duration: 2000 // 显示时长 2000ms
					});

				} else if (this.isQuitApply == '1' && this.ZT == '1') {

					uni.navigateTo({
						url: '../entrySuccess/entrySuccess?type=1'
					});
				} else if (this.isQuitApply == '1' && this.ZT == '2') {
					this.$refs.toast.show({

						content: '您还未进场',
						icon: 'error', // 不显示图标
						duration: 2000 // 显示时长 2000ms
					});

				}

			},
			startCountdown() {
				const intervalId = setInterval(() => {
					if (this.countdown > 0) {
						this.countdown--;
						this.remainingTime = this.countdown;
						this.buttonBackgroundColor = '#CCCCCC'; // 倒计时期间置灰
					} else {
						clearInterval(intervalId);
						this.isTimeZero = true;
						this.isButtonDisabled = false; // 倒计时结束，允许点击按钮
						this.buttonBackgroundColor = '#3A84FD'; // 恢复初始背景色
					}
				}, 1000);
			},
			confirmAndKnow() {
				this.entryShow = false; // 关闭弹窗
				uni.scanCode({
					success: function(res) {
						console.log('扫码成功11', res.result.result);

						// uni.navigateTo({
						// 	url: '../entry/entry'
						// });
					},
					fail: function(err) {
						console.error('扫码失败', err);
						// uni.navigateTo({
						// 	url: '../entry/entry'
						// });
					}
				});
				// 其他逻辑，如提交确认信息等
			},
			Entry() {

				var that = this;

				if (that.ZT !== '1') {
					if (that.isApproachApply == '0') {


						uni.scanCode({
							success: function(res) {

								let qrcodeResult = res.result;

								if (qrcodeResult.slice(-2) === "@@") {
									console.log('正确了')
									// 提取@@前的数据
									let dataBeforeAt = qrcodeResult.slice(0, -2);

									// 进行页面跳转并携带参数
									uni.navigateTo({
										url: '../entry/entry?data=' + dataBeforeAt
									});
								} else {

									that.$refs.toast.show({

										content: '请扫描正确的二维码',
										icon: 'error', // 不显示图标
										duration: 2000 // 显示时长 2000ms
									});
								}
								// uni.navigateTo({
								// 	url: '../entry/entry'
								// });
							},
							fail: function(err) {
								that.$refs.toast.show({

									content: '请扫描正确的二维码',
									icon: 'error', // 不显示图标
									duration: 2000 // 显示时长 2000ms
								});
								// uni.navigateTo({
								// 	url: '../entry/entry'
								// });
							}
						});
					} else {
						uni.navigateTo({
							url: '../entrySuccess/entrySuccess'
						});
					}
				} else {
					that.$refs.toast.show({

						content: '您已进场',
						icon: 'error', // 不显示图标
						duration: 2000 // 显示时长 2000ms
					});
				}




				// let a = 1;
				// if (a == 1) {
				// 	this.entryShow = true;
				// 	this.startCountdown();
				// }

			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100vh;
		/* 占据整个视口高度 */
		display: flex;
		flex-direction: column;
		position: relative;
		/* 相对定位，以便子元素可以相对于它定位 */
	}


	.content_top {

		position: relative;
		/* 设置相对定位，以便子元素可以相对于它定位 */
		width: 100%;
		/* 宽度百分之百 */
		height: 500rpx;
		/* 设置高度 */
		overflow: hidden;

		/* 触发 BFC */
		.background-image {
			position: absolute;
			/* 设置绝对定位，让背景图充满整个父容器 */
			top: 0;
			left: 0;
			width: 100%;
			/* 宽度百分之百 */
			height: 100%;
			/* 高度百分之百 */
			object-fit: cover;
			/* 图片填充父容器 */
			z-index: -1;
			/* 将背景图放到底层 */
		}

		.content_top_box {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 73rpx 73rpx 0 73rpx;

			.content_top_box_left {
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 48rpx;
				color: #FFFFFF;
			}

			.content_top_box_right {
				z-index: 99;
			}
		}
	}

	.content_bottom {
		flex: 1;
		/* 自动填充剩余空间 */
		position: absolute;
		/* 绝对定位，覆盖在 .content_top 上方 */
		top: 295rpx;
		/* 位于父容器顶部下方 200rpx 处 */
		width: 100%;
		/* 宽度百分之百 */
		height: calc(100% - 295rpx);
		/* 高度占满父容器下方 */
		background-color: #F8F9FD;
		/* 背景色，可根据需要修改 */
		z-index: 0;
		border-top-left-radius: 50rpx;
		border-top-right-radius: 50rpx;

		/* 确保在 content_top 之下 */
		// display: flex;
		// justify-content: center;
		// align-items: center;
		.content_bottom_xm {
			background: #F1FAFF;
			box-shadow: 0rpx 7rpx 10rpx 0rpx rgba(0, 0, 0, 0.04);
			border-radius: 16rpx;
			margin: 33rpx;
			position: relative;
			/* padding: 52rpx;wxcs_style_padding; */
			padding: 28rpx 27rpx 28rpx 27rpx;

			.content_bottom_xm_name {
				font-family: PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #3A3A3A;
			}

			.content_bottom_xm_fjname {
				font-family: PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #666666;
				margin-top: 28rpx;
			}

			.content_bottom_xm_dept {
				font-family: PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #666666;
				margin-top: 28rpx;
			}

			.content_bottom_xm_time {
				font-family: PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #666666;
				margin-top: 28rpx;
			}

			.content_bottom_xm_img {
				position: absolute;
				right: 37rpx;
				bottom: 37rpx;
			}
		}

		.content_bottom_title {

			font-family: PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #999999;
			/* margin-top: auto; */
			text-align: center;
			position: absolute;
			bottom: 37rpx;
			left: 27%;
		}

		.content_bottom_list {
			margin: 33rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.content_bottom_list_dj {
				display: flex;
				margin: 16rpx 0;
				align-items: center;
				justify-content: space-between;
				background: #FFFFFF;
				box-shadow: 0rpx 10rpx 20rpx 0rpx rgba(0, 0, 0, 0.06);
				border-radius: 21rpx;
				padding: 38rpx;

				.content_bottom_list_dj_left {
					.content_bottom_list_dj_left_name {
						font-family: PingFang SC;
						font-weight: bold;
						font-size: 36rpx;
						color: #333333;
					}

					.content_bottom_list_dj_left_detail {
						font-family: PingFang SC;
						font-weight: 500;
						font-size: 24rpx;
						color: #999999;
						margin-top: 24rpx;
					}
				}

				.content_bottom_list_dj_right {
					margin-left: 14rpx;
				}
			}
		}

	}

	.confrim-btn {
		text-align: center;
		margin-top: 10rpx;
	}

	.contentpopur {
		padding: 55rpx;

		.contentpopur_title {
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #111111;
			text-align: center;
		}

	}
</style>